#pragma once
#include "position.h"
#include "global_config.h"

//POSITION
Position _position_set_world(V2 world_point,Position & self)
{
	self.world = world_point;
	self.chunk_index = v2i(world_point.x / (G::CHUNK_SIZE_PIXELS),world_point.y / (G::CHUNK_SIZE_PIXELS));
	self.relative = world_point - self.chunk_index*v2(G::CHUNK_SIZE_PIXELS,G::CHUNK_SIZE_PIXELS);
	math_cap_values(self.relative,v2(0,0),v2(G::CHUNK_SIZE_PIXELS));
	return self;
}

V2 position_get_screen_pos(Position p, V2 camera_pos)
{
	V2 Result = p.world - camera_pos + v2i(G::GRAPHICS_RENDERWINDOW_TOP_LEFT);
	return Result;
}

Position position_set(V2 screen_pos, Position & in_out_pos, V2 camera_pos)
{
	return _position_set_world(screen_pos + camera_pos - v2i(G::GRAPHICS_RENDERWINDOW_TOP_LEFT),in_out_pos);
}

Position position_set(V2 world_pos, Position & self)
{
	return _position_set_world(world_pos,self);
}

Position position_set(V2i chunk_id, V2 rel_pos, Position & self)
{

	V2 world_pos = (V2)chunk_id * v2((float)G::CHUNK_SIZE_PIXELS) +rel_pos;
	math_cap_values(world_pos,v2(0),v2((float)(G::CHUNK_SIZE_PIXELS * (G::CHUNK_COUNT-1))));
	return _position_set_world(world_pos,self);
}

